//
//  SceneDelegate.swift
//  WatchExample
//
//  Created by Longxiang Guo (QXQ4633) on 2021/6/14.
//

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    var session: UISceneSession?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        self.session = session
        guard let _ = (scene as? UIWindowScene) else { return }
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
        print(self.session)
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        print(self.session )
         
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
        // Tenserflow
    }
}

/// App statck trace when enter background, see ali t & c
///
/**
 _initialSettingsDiffActions = 0x0000600003b37200 7 elements
 _finalSettingsDiffActions = 0x00007fff8002e300 0 elements
 _initialSceneBSActionHandlers = 0x0000600000d6dcc0 5 elements
 _finalSceneBSActionHandlers = 0x000060000182e4a0 2 elements
 _additionalSettingsDiffActions = 0x00007fff8019f440
 _flattenedDiffActions = 0x000060000182ec20
 _additionalSceneBSActionHandlers = 0x00007fff8019f440
 _flattenedActionsHandlers = 0x000060000182ec80
 _builtinComponents = nil
 _registeredComponents = 0x000060000182eaa0
 _lifecycleMonitor = 0x000060000182e560
 _overrideSettings = nil
 _postSettingsUpdateResponseBlocks = 0x0000600001817220 1 key/value pair
 _settingsScene = nil
 _inheritingScenes = 0x0000600002934320
 _sceneIdentifier = nil
 _persistenceIdentifier = nil
 _oldSettings = 0x0000600003614800
 _respondingToLifecycleEvent = true
 __cachedInterfaceOrientation = 0xd73a59172c9f0f36 Int64(1)
 ///
 /// */
/**
 thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 5.1
   * frame #0: 0x00000001023c480c WatchExample`SceneDelegate.sceneDidEnterBackground(scene=0x00007fa245f063c0, self=0x0000600001eb68c0) at SceneDelegate.swift:48:5
     frame #1: 0x00000001023c4854 WatchExample`@objc SceneDelegate.sceneDidEnterBackground(_:) at <compiler-generated>:0
     frame #2: 0x00007fff23c900df UIKitCore`-[_UISceneLifecycleMonitor didEnterBackground] + 171
     frame #3: 0x00007fff23c91177 UIKitCore`-[_UIWindowSceneFBSSceneLifecycleMonitor didEnterBackground] + 50
     frame #4: 0x00007fff2421f4ba UIKitCore`_UIScenePerformActionsWithLifecycleActionMask + 88
     frame #5: 0x00007fff23c90ca9 UIKitCore`__111-[_UIWindowSceneFBSSceneLifecycleMonitor transitionToTargetState:fromState:withTransitionContext:preparations:]_block_invoke.112 + 156
     frame #6: 0x00007fff23c9f39d UIKitCore`___UISceneLifecycleSettingsUpdateBlockWithCanvasAndTransitionContext_block_invoke_2 + 463
     frame #7: 0x00007fff23c9077d UIKitCore`-[_UIWindowSceneFBSSceneLifecycleMonitor transitionToTargetState:fromState:withTransitionContext:preparations:] + 953
     frame #8: 0x00007fff23c9f008 UIKitCore`___UISceneLifecycleSettingsUpdateBlockWithCanvasAndTransitionContext_block_invoke + 370
     frame #9: 0x00007fff23ca093d UIKitCore`__186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke + 125
     frame #10: 0x00007fff24129a73 UIKitCore`+[BSAnimationSettings(UIKit) tryAnimatingWithSettings:actions:completion:] + 871
     frame #11: 0x00007fff2423be3e UIKitCore`_UISceneSettingsDiffActionPerformChangesWithTransitionContext + 240
     frame #12: 0x00007fff23ca0713 UIKitCore`-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:] + 352
     frame #13: 0x00007fff23ac6dc7 UIKitCore`__64-[UIScene scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke + 797
     frame #14: 0x00007fff23ac5871 UIKitCore`-[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:] + 253
     frame #15: 0x00007fff23ac6a00 UIKitCore`-[UIScene scene:didUpdateWithDiff:transitionContext:completion:] + 208
     frame #16: 0x00007fff24152197 UIKitCore`-[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] + 502
     frame #17: 0x00007fff25a5695f FrontBoardServices`-[FBSScene updater:didUpdateSettings:withDiff:transitionContext:completion:] + 530
     frame #18: 0x00007fff25a80215 FrontBoardServices`__94-[FBSWorkspaceScenesClient _queue_updateScene:withSettings:diff:transitionContext:completion:]_block_invoke_2 + 133
     frame #19: 0x00007fff25a63ba1 FrontBoardServices`-[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 209
     frame #20: 0x00007fff25a8015d FrontBoardServices`__94-[FBSWorkspaceScenesClient _queue_updateScene:withSettings:diff:transitionContext:completion:]_block_invoke + 372
     frame #21: 0x00000001026d2a88 libdispatch.dylib`_dispatch_client_callout + 8
     frame #22: 0x00000001026d59d0 libdispatch.dylib`_dispatch_block_invoke_direct + 295
     frame #23: 0x00007fff25aa4901 FrontBoardServices`__FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
     frame #24: 0x00007fff25aa45e7 FrontBoardServices`-[FBSSerialQueue _targetQueue_performNextIfPossible] + 433
     frame #25: 0x00007fff25aa4aac FrontBoardServices`-[FBSSerialQueue _performNextFromRunLoopSource] + 22
     frame #26: 0x00007fff203a8845 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
     frame #27: 0x00007fff203a873d CoreFoundation`__CFRunLoopDoSource0 + 180
     frame #28: 0x00007fff203a7c1f CoreFoundation`__CFRunLoopDoSources0 + 248
     frame #29: 0x00007fff203a23f7 CoreFoundation`__CFRunLoopRun + 878
     frame #30: 0x00007fff203a1b9e CoreFoundation`CFRunLoopRunSpecific + 567
     frame #31: 0x00007fff2b76edb3 GraphicsServices`GSEventRunModal + 139
     frame #32: 0x00007fff24660c73 UIKitCore`-[UIApplication _run] + 912
     frame #33: 0x00007fff24665b84 UIKitCore`UIApplicationMain + 101
     frame #34: 0x00007fff5387e7b2 libswiftUIKit.dylib`UIKit.UIApplicationMain(Swift.Int32, Swift.Optional<Swift.UnsafeMutablePointer<Swift.UnsafeMutablePointer<Swift.Int8>>>, Swift.Optional<Swift.String>, Swift.Optional<Swift.String>) -> Swift.Int32 + 98
     frame #35: 0x00000001023c3a4a WatchExample`static UIApplicationDelegate.main() at <compiler-generated>:0
     frame #36: 0x00000001023c39be WatchExample`static AppDelegate.$main(self=WatchExample.AppDelegate) at <compiler-generated>:0
     frame #37: 0x00000001023c3a99 WatchExample`main at <compiler-generated>:0
     frame #38: 0x00007fff20257415 libdyld.dylib`start + 1
     frame #39: 0x00007fff20257415 libdyld.dylib`start + 1
 */
